//index.js
//获取应用实例
const app = getApp()
const request = require("../../utils/request.js");
const CountryCode = require("../../utils/countryCode.js")
Page({
  data: {
    inputText: "输入搜索",
    hidden: true,
    scrollTop: 0,
    scrollHeight: 0,
    arr: [],
    arrHeight: [],
    pageToken: 1,
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    //本地数据
    items: [],
    countryCode: [],
  },
  //载入页面时加载
  onLoad: function() {
    var that = this;
    wx.getSystemInfo({
      success: function(res) {
        console.log(res.windowHeight);
        console.log(res.windowWidth);
        that.setData({
          scrollHeight: res.windowHeight
        });
      },
    });
    var url = request.URL + "/index";
    var items = [];
    var banners = [];
    request.request({
      url: url,
      method: 'POST',
      data: {

      },
      success: function(res) {
        items = res.data.indexGoods;
        banners = res.data.banners;
        for (var i = 0; i < items.length; i++) {
          var itemCountryId = items[i].countryId;
          var countryInfo = CountryCode.getCountryInfo(itemCountryId) || null;
          if (countryInfo == null) {
            items[i].countryName = "其它";
            items[i].countryImgUrl = countryImgUrl;
          } else {
            items[i].countryName = countryInfo.chineseName;
            items[i].countryImgUrl = countryInfo.img;
          }
        }

        that.setData({
          banners: banners,
          items: items
        })
        console.log(banners);
        console.log(items);
        //that.data.items[1].countryId
        // console.log(that.data.countryCode[that.data.items[1].countryId-1].chineseName)
      },
      fail: function() {
        request.showFailInfo()
      }
    })

  },
  //打开相机扫描
  openScanBarcode: function(e) {
    wx.scanCode({
      success: (res) => {
        console.log(res)
      }
    })
  },

  accessToItem: function(e) {
    var that = this;
    var clickedId = e.currentTarget.id
    var arr = that.data.items;
    console.log(arr);
    var url = '';
    for (var i = 0; i < arr.length; i++) {
      var goodId = arr[i].goodId;
      console.log(goodId);
      if (goodId == clickedId) {
        url = "../../pages/details/item_details/item_details?goodId=" + goodId;
        break;
      }
    }
    wx.navigateTo({
      url: url,
    })
    console.log(url);
  },

  //转发小程序
  onShareAppMessage: function() {
    return {
      title: '呆狗商城',
      path: '/pages/index/index',
      success: function(res) {
        //转发成功
        wx.showToast({
          title: '转发成功',
          icon:'success'
        })
      },
      fail: function(res) {
        //转发失败
      }
    }
  },
  scroll: function(e, res) {
    if (e.detail.scrollTop > 400) {
      this.setData({
        floorstatus: true
      });
    } else {
      this.setData({
        floorstatus: false
      });
    }
  },
  goTop: function(e) {
    this.setData({
      scrollTop: 0
    });
  },
  toSearchPage: function(e) {
    console.log(e.currentTarget.dataset.title)
    wx.navigateTo({
      url: '../../pages/search/search' + '?storeId=' + -1,
    })
  },
  //打开呆狗背包页
  accessBeibaoPage: function(e) {
    wx.navigateTo({
      url: '../../pages/beibao/beibao'
    })
  },
  toBannerItem: function(e) {
    // var that = this;
    // var id = e.currentTarget.dataset.set;
    // var banners = that.data.banners;
    // var skipUrl = that.data.banners[id].bannerSkipUrl;
    // console.log(skipUrl)
  },

  //到达页面最下面时候加载
  onReachBottom: function(e) {
    request.request({
      url: 'http://baike.baidu.com/api/openapi/BaikeLemmaCardApi',
      data: {
        "scope": "103",
        "format": "json",
        "appid": "379020",
        "bk_key": "银魂",
        "bk_length": "600"
      },
      success: function(res) {
        console.log(res)
      },
      fail: function() {
        console.log("fail")
      }
    })
  },
  onPullDownRefresh: function(e) {
    console.log("好用不?")
    wx.showToast({
      title: '没事儿别乱拉', //提示信息
      icon: 'success', //成功显示图标
      duration: 2000 //时间
    })
  },
})